package com.ca.ui.core.view.card;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ui.core.R;
import com.ca.ui.core.adapter.BannerImageAdapter;
import com.ca.ui.core.linstener.BannerCardOnPageChangeListener;
import com.ca.ui.core.util.ImageUtils;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.transformer.AlphaPageTransformer;

import java.util.ArrayList;
import java.util.Arrays;

public class BannerCard extends ConstraintLayout {

    private String[] imageUrlArray;

    public ImageView imageView;
    public Banner<String, BannerAdapter<String, BannerImageHolder>> banner;

    public BannerAdapter<String, BannerImageHolder> adapter;

    public BannerCard(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public BannerCard(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public BannerCard(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        View view = LayoutInflater.from(context).inflate(R.layout.banner_card_layout, this);

        imageView = view.findViewById(R.id.iv);
        banner = view.findViewById(R.id.banner);

        adapter = new BannerImageAdapter(context, new ArrayList<>());

        banner.setAdapter(adapter)
                .setIndicator(new CircleIndicator(context))
                .setBannerRound(30)
                .setPageTransformer(new AlphaPageTransformer());

        //  设置长宽比 21:7
        banner.post(() -> {
            int width = banner.getWidth();
            int height = (int) (width * (9.0 / 18));
            ViewGroup.LayoutParams params = banner.getLayoutParams();
            params.height = height;
            banner.setLayoutParams(params);
        });
    }

    public String[] getImageUrlArray() {
        return imageUrlArray;
    }

    public void setImageUrlArray(String[] imageUrlArray) {
        this.imageUrlArray = imageUrlArray == null ? new String[0] : imageUrlArray;
        adapter.setDatas(Arrays.asList(this.imageUrlArray));
        if (this.imageUrlArray.length > 0) {
            ImageUtils.loadFuzzyImage(getContext(), this.imageUrlArray[this.imageUrlArray.length - 1], imageView, 8);
            imageView.post(() -> {
                int width = imageView.getWidth();
                ViewGroup.LayoutParams params = imageView.getLayoutParams();
                params.height = (int) (width * 2.0 / 3);
                imageView.setLayoutParams(params);
            });
        }
    }

    public void addOnPageChangeListener(BannerCardOnPageChangeListener listener) {
        banner.addOnPageChangeListener(listener);
    }
}
